package com.example.peanutsubway.dao.impl;

import com.example.peanutsubway.dao.BookInfoDao;
import com.example.peanutsubway.entity.BookInfo;
import com.example.peanutsubway.entity.BookType;
import com.example.peanutsubway.util.JDBCUtils;
import com.example.peanutsubway.util.PageInfo;
import com.example.peanutsubway.vo.BookTypeInfo;

import java.util.List;

/**
 * @Author：zzt
 * @Version：1.0
 * @Date：2021/10/12-10:08
 * @Since:jdk1.8
 * @Description:
 */
public class BookInfoDaoImpl implements BookInfoDao {


    @Override
    public List<BookInfo> findAllBook(BookInfo bookInfo, PageInfo pageInfo) {
        StringBuilder sql = new StringBuilder("SELECT b.bid,b.bookid,b.bookname,b.Authorname,b.fTypeid,t.booktypename,b.Stypeid,t2.booktypename parentname,b.bookStatus FROM pn_book b,pn_book_type t,pn_book_type t2 WHERE b.fTypeid = t.booktypeid AND b.Stypeid = t2.booktypeid AND t.booktypeid = b.fTypeid ");
        if (bookInfo != null) {
            if (bookInfo.getBookName() != null && !"".equals(bookInfo.getBookName())) {
                sql.append(" and b.bookname like '%" + bookInfo.getBookName() + "%' ");
            }
            if (bookInfo.getBookId()!=0){
                sql.append(" and b.bookid = "+ bookInfo.getBookId());
            }
            if (bookInfo.getFTypeId()!=0){
                sql.append(" and b.fTypeid = " + bookInfo.getFTypeId());
            }
            if (bookInfo.getStypeId()!=0){
                sql.append(" and b.Stypeid = " + bookInfo.getStypeId());
            }
        }
        sql.append(" limit ?,?");
        return JDBCUtils.find(sql.toString(), BookTypeInfo.class,pageInfo.getStartIndex(),pageInfo.getPageNum());
    }

    @Override
    public List<BookType> findAllBookParentType() {
        String sql = "SELECT * FROM pn_book_type where parentid = 0";
        return JDBCUtils.find(sql,BookType.class);
    }

    @Override
    public List<BookType> findAllBookSonType(Integer id) {
        if (id!=-1){
            String sql = "SELECT p.* FROM pn_book_type p WHERE p.parentid = ? AND p.parentid <> 0";
            return JDBCUtils.find(sql,BookType.class,id);
        }else{
            String sql = "SELECT p.* FROM pn_book_type p WHERE p.parentid <> 0";
            return JDBCUtils.find(sql,BookType.class);
        }


    }


    @Override
    public Integer count(BookInfo bookInfo) {
        StringBuilder sql = new StringBuilder("select count(1) from pn_book where 1=1 ");
        if (bookInfo != null) {
            if (bookInfo.getBookName() != null && !"".equals(bookInfo.getBookName())) {
                sql.append(" and bookname like '%" + bookInfo.getBookName() + "%' ");
            }
            if (bookInfo.getBookId()!=0){
                sql.append(" and bookid = "+ bookInfo.getBookId());
            }
            if (bookInfo.getFTypeId()!=0){
                sql.append(" and fTypeid = " + bookInfo.getFTypeId());
            }
            if (bookInfo.getStypeId()!=0){
                sql.append(" and Stypeid = " + bookInfo.getStypeId());
            }
        }
        return JDBCUtils.count(sql.toString()).intValue();
    }

    @Override
    public Boolean updateBookInfo(BookInfo bookInfo) {
        String sql = "UPDATE pn_book SET bookName = ?,AuthorName = ?,fTypeid = ?,Stypeid = ?,bookStatus = ? WHERE bookId = ?";
        return JDBCUtils.update(sql,bookInfo.getBookName(),bookInfo.getAuthorName(),bookInfo.getFTypeId(),bookInfo.getStypeId(),bookInfo.getBookStatus(),bookInfo.getBookId())>0;
    }

    @Override
    public Boolean deleteBookSonType(Integer id) {
        String sql = "DELETE FROM pn_book_type WHERE booktypeid = ?";
        return JDBCUtils.update(sql)>0;
    }

    @Override
    public Boolean addBookInfo(BookInfo bookInfo) {
        String sql = "INSERT INTO pn_book(bookName,fTypeId,stypeId,authorName,bookStatus) VALUES(?,?,?,?,?)";
        return JDBCUtils.update(sql,bookInfo.getBookName(),bookInfo.getFTypeId(),bookInfo.getStypeId(),bookInfo.getAuthorName(),bookInfo.getBookStatus())>0;
    }

}
